
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

class mapHeader {
public:
    void add(std::string s) {
        header.push_back(s);
    }
    std::vector<std::string> header;
};


class userDataVector {
public:
    void add(std::vector<std::string> s) {
        data.push_back(s);
    }

    std::vector<std::vector<std::string>> data;
};


mapHeader h;
userDataVector dataVector;
userDataVector WrongDataVexror;
int numberOfHeader = 0;

void headerIndexRow(const std::string& filePath,int headerIndex) {
    std::ifstream file(filePath);
    std::string line;
    for (int i = 0; i < headerIndex; ++i) {
        if (!std::getline(file, line)) {
            std::cerr << "Error reading file: " << filePath << std::endl;
            file.close();
            return;
        }
    }
    std::istringstream iss(line);
    std::string field;

    while (std::getline(iss, field, ',')) {
        h.add(field);
        numberOfHeader++;
    }
    file.close();
}

void loadUserDataVector(const std::string& filePath) {
    std::ifstream file(filePath);

    // 读取文件内容并打印
    std::string line;
    while (std::getline(file, line)) {
        // 使用字符串流解析CSV行
        std::istringstream iss(line);
        std::string field;
        std::vector<std::string> fields;

        while (std::getline(iss, field, ',')) {
            fields.push_back(field);
        }
        dataVector.add(fields);
    }
    file.close();
}
void loadWorngUserDataVector(const std::string& filePath) {
    std::ifstream file(filePath);
    std::string line;
    while (std::getline(file, line)) {
        // 使用字符串流解析CSV行
        std::istringstream iss(line);
        std::string field;
        std::vector<std::string> fields;

        while (std::getline(iss, field, ',')) {
            fields.push_back(field);
        }
        WrongDataVexror.add(fields);
    }
    file.close();
}

void printAll(){
    for (const auto& row : dataVector.data) {
        for (const auto& element : row) {
            std::cout << element << "\t";
        }
        std::cout << std::endl;
    }
}
void printLine(std::string line){
    for (const auto& row : dataVector.data) {
        for (const auto& element : row) {
            if(element == line){
                for (int i = 0; i < numberOfHeader; ++i) {
                    std::cout << h.header[i] <<":"<<row[i] << "\t"<<std::endl;
                }
            }
        }

    }
}
void printAnyLine(short line1,short line2){
    std::cout<<dataVector.data[line1-1][line2-1]<<std::endl;
}
void chick(const std::string& filePath1,const std::string& filePath2) {
    std::vector<std::string> repeatData;
    std::cout<<"在源文件中"<<std::endl;
    for (int i = 3; i < dataVector.data.size(); ++i){
        //检查是否有重复的数据
        for (int j = i+1; j < dataVector.data.size(); ++j) {
            if (dataVector.data[i][2] == dataVector.data[j][2]){
                std::cout<<dataVector.data[i][2] << "是重复的数据"<<std::endl;
                repeatData.push_back(dataVector.data[i][2]);
            }
        }
    }
    std::cout<<std::endl;
    std::cout<<"在检查文件中"<<std::endl;

    for (int i = 3; i < WrongDataVexror.data.size(); ++i){
        //检查是否有重复的数据
        for (int j = i+1; j < WrongDataVexror.data.size(); ++j) {
            if (WrongDataVexror.data[i][2] == WrongDataVexror.data[j][2]){
                std::cout<<WrongDataVexror.data[i][2] << "是重复的数据"<<std::endl;
            }
        }
    }
    std::cout<<std::endl;


    std::cout<<"在解析数据中"<<std::endl;
    for (int i = 3; i < dataVector.data.size(); ++i) {
        for (int j = 3; j < WrongDataVexror.data.size(); ++j) {
            if ( !repeatData.empty()){
                for (int k = 0; k < repeatData.size(); ++k) {
                    if (dataVector.data[i][2] == repeatData[k]){

                    }else{
                        if (dataVector.data[i][2] == WrongDataVexror.data[j][2]){
                            for (int k = 0; k < numberOfHeader; ++k) {
                                if (dataVector.data[i][k] != WrongDataVexror.data[j][k]){
                                    std::cout<<dataVector.data[i][2] << "的"<<h.header[k]<<"数据不一致，正确的为"<<dataVector.data[i][k]<<std::endl;
                                }
                            }
                        }
                    }
                }
            } else{
                std::cout<<dataVector.data[i][2] << "的"<<h.header[j]<<"数据不一致"<<std::endl;
            }

//            if (dataVector.data[i][2] == WrongDataVexror.data[j][2]){
//                for (int k = 0; k < numberOfHeader; ++k) {
//                    if (dataVector.data[i][k] != WrongDataVexror.data[j][k]){
//                        std::cout<<dataVector.data[i][2] << "的"<<h.header[k]<<"数据不一致"<<std::endl;
//                    }
//                }
//            }
        }
    }
}
int main() {
    std::string filePath = R"(D:\c++\作业(2)\作业\code\original_data.csv)";
    std::string filePath2 = R"(D:\c++\作业(2)\作业\code\data_for_check.csv)";
    int headerIndex = 2;

    headerIndexRow(filePath,headerIndex);  // 读取第几行当索引的
    loadUserDataVector(filePath);  // 读取所有数据，并制表
    loadWorngUserDataVector(filePath2);  // 读取所有数据，并制表

    //printAll();//打印全部数据
    //printLine("5");//打印第五行的数据
   // printAnyLine(5,3);
    //chick(filePath,filePath2);
    return 0;
}


